package club.mzywucai.blog.file_system.pojo.domain;

import club.mzywucai.blog.file_system.util.DateUtil;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;

/**
 * @author mzywucai
 * @Description
 * @date 2019/1/30
 */
@Component
@PropertySource({"classpath:config/application.properties"})
public class MakeDir {
    /**
     * @Description 用户头像图片的Path。
     */
    @Value("${server.photo.base}")
    private String photoPath;

    public String getPhotoPath() {
        return photoPath;
    }

    /**
     * @Description 持久化最终的位置的base：文档类
     */
    @Value("${server.document.base}")
    private String documentPath;
    /**
     * @Description 持久化最终的位置的base：系统类
     */
    @Value("${server.system.base}")
    private String systemPath;
    /**
     * @Description 持久化最终的位置的base：安装包类
     */
    @Value("${server.package.base}")
    private String packagePath;
    /**
     * @Description 持久化最终的位置的base：音频类
     */
    @Value("${server.audio.base}")
    private String audioPath;

    /**
     * @Description 常量字段：文档类
     */
    @Value("${server.document.uri}")
    private String documentURI;
    /**
     * @Description 常量字段：系统类
     */
    @Value("${server.system.uri}")
    private String systemURI;
    /**
     * @Description 常量字段：安装包类
     */
    @Value("${server.package.uri}")
    private String packageURI;
    /**
     * @Description 常量字段：音频类
     */
    @Value("${server.audio.uri}")
    private String audioURI;

    private String todayDocumentPath;
    private String todaySystemPath;
    private String todayPackagePath;
    private String todayAudioPath;

    private String todayDocumentURI;
    private String todaySystemURI;
    private String todayPackageURI;
    private String todayAudioURI;

    public String getDocumentURI() {
        return documentURI;
    }

    public void setDocumentURI(String documentURI) {
        this.documentURI = documentURI;
    }

    public String getSystemURI() {
        return systemURI;
    }

    public void setSystemURI(String systemURI) {
        this.systemURI = systemURI;
    }

    public String getPackageURI() {
        return packageURI;
    }

    public void setPackageURI(String packageURI) {
        this.packageURI = packageURI;
    }

    public String getAudioURI() {
        return audioURI;
    }

    public void setAudioURI(String audioURI) {
        this.audioURI = audioURI;
    }

    public String getTodayDocumentPath() {
        return todayDocumentPath;
    }

    public String getTodaySystemPath() {
        return todaySystemPath;
    }

    public String getTodayPackagePath() {
        return todayPackagePath;
    }

    public String getTodayAudioPath() {
        return todayAudioPath;
    }

    public String getTodayDocumentURI() {
        return todayDocumentURI;
    }

    public String getTodaySystemURI() {
        return todaySystemURI;
    }

    public String getTodayPackageURI() {
        return todayPackageURI;
    }

    public String getTodayAudioURI() {
        return todayAudioURI;
    }

    /**
     * @Description 每天凌晨12点创建
     * @author mzywucai
     * @time 2019-01-30
     * @see 
     * @return   void
     * @params  []
     */
    @Scheduled(cron = "0 0 0 * * ?")
    public void mkdir() throws Exception {
        File file = new File(photoPath);
        file.mkdirs();
        if (!file.exists()) {
            System.err.println("此计算机系统上无权限创建目录<"+file.getAbsolutePath()+">");
            throw new Exception("此计算机系统上无权限创建目录");
        }


        Date date = new Date();
        int year = DateUtil.getYear(date);
        int month = DateUtil.getMonth(date);
        int day = DateUtil.getDay(date);
        // 创建目录层级
        String todayPathURI = "/"+year+"/"+month+"/"+day+"/";
        // 本地文件系统的Path
        todayDocumentPath = documentPath + todayPathURI;
        todaySystemPath = systemPath + todayPathURI;
        todayPackagePath = packagePath + todayPathURI;
        todayAudioPath = audioPath + todayPathURI;
        // Http访问的URI
        todayDocumentURI = documentURI + todayPathURI;
        todaySystemURI = systemURI + todayPathURI;
        todayPackageURI = packageURI + todayPathURI;
        todayAudioURI = audioURI + todayPathURI;

        File[] files = new File[4];
        files[0] = new File(todayDocumentPath);
        files[1] = new File(todaySystemPath);
        files[2] = new File(todayPackagePath);
        files[3] = new File(todayAudioPath);

        for (File f : files) {
            // 确保不存在目录，才创建目录
            if (!f.exists()) {
                f.mkdirs();
            }
            // 检查创建是否成功
            if (!f.exists()) {
                System.err.println("此计算机系统上无权限创建目录<"+f.getAbsolutePath()+">");
                throw new Exception("此计算机系统上无权限创建目录");
            }
        }


    }
}
